﻿var SearchObject = function () {

    this.searchCall = null; //the function that will be called if something needs to be searched.
    this.searchRemoveCall = null;//the functon that will be called when the search is removed
    this.searchText = ''

    this.AllTabText = {
        normal: 'All'
		, filtered: 'Search Results'
    };

    this.Error = new ErrorContainerObject();
    this.intMinCharAtKeyword = 3;
    this.isEnabled = true;

    /********************************************************************************
	These variables are commented out, because they need to be added using prototype.
	---------------------------------------------------------------------------------
	
	this.defaultSearchText = '';
	this.inactiveBoxClassName = '';
	this.activeBoxClassName = '';
	this.inactiveButtonClassName = '';
	this.activeButtonClassName = '';
	
	********************************************************************************/

    var This = this
		, isActive = false //is the search box active or not i.e. has the user typed something into it or not
		, box = $('#SearchBox') //the search box
		, button = $('#SearchButton') //the search button
		, Buttons = {
		    ENTER: 13
		}
		, searchFilter = $('#SearchFilter')
		//,searchFilterText = $('#SearchFilterText')
		, All = $('#AllSelector')
		, TypeTab = $('#liByType')
		, InsidersTab = $('#liInsiders')
    ;

    this.remove = function () {
        Utility.blur()

        searchFilter.stop(true, false).animate({
            opacity: 0
			, height: 0
        }, null, null, function () {
            searchFilter.css({
                display: 'none'
            });
        });

        All.text(this.AllTabText.normal);
        TypeTab.show();
        if (IS_SEC) {
            InsidersTab.show();
        }
        makeBoxUnactive();

        this.searchText = '';
        if (typeof this.searchRemoveCall == 'function') {
            this.searchRemoveCall();
        }
    };

    function _init() {

        if (typeof This.defaultSearchText != 'string')
            This.defaultSearchText = 'Keyword';

        if (typeof This.inactiveButtonClassName != 'string')
            This.inactiveButtonClassName = 'InactiveButton';
        if (typeof This.activeButtonClassName != 'string')
            This.activeButtonClassName = 'ActiveButton';

        if (typeof This.inactiveBoxClassName != 'string')
            This.inactiveBoxClassName = 'InactiveInputBox';
        if (typeof This.activeBoxClassName != 'string')
            This.activeBoxClassName = 'ActiveInputBox'

        makeBoxUnactive();

        box.mousedown(boxClicked).keydown(function (e) {
            if (e.which == Buttons.ENTER) {
                search(e);
            }
        });
        button.mousedown(search);

        //adds the [String].trim() method to the [String] object if missing
        if (!String.prototype.trim) {
            String.prototype.trim = function () {
                return this.replace(/^\s+|\s+$/g, '');
            };
        }

        searchFilter.css({
            display: 'none'
        }).animate({
            opacity: 0
			, height: 0
        }, 0);

        $(document).mousedown(documentClick);
    }

    function search(e) {
        e.stopPropagation();

        box.blur();

        if (!isActive || !This.isEnabled)
            return;
        var value = box[0].value.trim();
        if (!value.length) {
            makeBoxUnactive();
            return;
        }

        if (value.length < This.intMinCharAtKeyword) {
            This.Error.show();
            makeBoxUnactive();
            return;
        }

        This.Error.hide();
        TypeTab.hide();
        InsidersTab.hide();//add more this
        if (IS_SEC) {
            InsidersTab.hide();
        }

        var intHeight,
			oldOpacity = searchFilter.css('opacity');
        oldHeight = searchFilter.height();
        

        This.searchText = value;
        if (typeof This.searchCall == 'function') {
            This.searchCall(value);
        }

        //searchFilterText.text(value);	

        searchFilter.stop(true, false).css({
            position: 'absolute'
			, top: '0px'
			, left: '0px'
			, height: 'auto'
			, display: 'block'
			, width: searchFilter.width() + 'px'
			, opacity: 0.01
        });

        intHeight = searchFilter.height();

        searchFilter.css({
            position: 'static'
			, top: 'auto'
			, left: 'auto'
			, width: 'auto'
			, height: oldHeight + 'px'
			, opacity: oldOpacity
        }).animate({
            opacity: 1,
            height: intHeight + 'px'
        }, null, null, function () {
            searchFilter.css({ height: 'auto' });
        });

        All.text(This.AllTabText.filtered);
    }
    function boxClicked(e) {
        e.stopPropagation();

        box.removeClass(This.inactiveBoxClassName).addClass(This.activeBoxClassName);
        if (!isActive)
            box[0].value = '';
        isActive = true;
        button.removeClass(This.inactiveButtonClassName).addClass(This.activeButtonClassName);
    }
    function makeBoxUnactive() {
        isActive = false;
        box.removeClass(This.activeBoxClassName).addClass(This.inactiveBoxClassName);
        button.removeClass(This.activeButtonClassName).addClass(This.inactiveButtonClassName);
        box[0].value = This.defaultSearchText;
    }
    function documentClick() {
        if (isActive) {
            box[0].value = box[0].value.trim();
            if (!box[0].value.length) {
                makeBoxUnactive();
            }
        }
    }
    _init();
};